
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.*;
import java.awt.geom.*;

import javax.swing.*;

public class e2GUIParameters {

    private static final long serialVersionUID = 42L;
    Image[] patternimage;
    Image[][][] pieceimage;
    e2Applet topapplet;
    int i, j;
    int color1, color2, color3, color4;
    Graphics2D tempgraphic;
    Graphics2D tempg2;
    int number_of_patterns;
    int number_of_pieces;
    // Applet dimension
    int appletWidth;
    int appletHeight;
    //display coordinates
    int rownb;
    int colnb;
    int board_x;
    int board_y;
    int piece_w;
    int piece_h;
    //piece managing
    int need_regenerate;
    int loaded_patterns;
	//piece drag and drop
	boolean drag;
	boolean released;
	boolean redraw_piecebag;
	boolean redraw_boardpane;
	boolean mode_auto;
	e2piece dragged_piece;
	e2piece released_piece;
	boolean selection;
	int selection_col;
	int selection_row;
	boolean piecebag_lockpositions;
    

    public e2GUIParameters(e2Applet in_applet) {
        topapplet = in_applet;
        board_x = 0;
        board_y = 0;
        piece_w = 32; //64 Max ???;
        piece_h = 32; //64;
        rownb = 16;
        colnb = 16;
        need_regenerate = 0;
        loaded_patterns = 0;
        redraw_boardpane = true;
        redraw_piecebag = true;
        mode_auto = false;
        appletWidth = piece_w * colnb; //1000;
        appletHeight = piece_h * rownb; //1000;
        number_of_patterns = 23;
        number_of_pieces = 256;
        selection = false;
        selection_row = 0;
        selection_col = 0;
        piecebag_lockpositions = false;
        patternimage = new Image[number_of_patterns + 1];
        patternimage[1] = topapplet.getImage(topapplet.getCodeBase(), "patterns/edge/X.png");
        patternimage[2] = topapplet.getImage(topapplet.getCodeBase(), "patterns/A.png");
        patternimage[3] = topapplet.getImage(topapplet.getCodeBase(), "patterns/B.png");
        patternimage[4] = topapplet.getImage(topapplet.getCodeBase(), "patterns/C.png");
        patternimage[5] = topapplet.getImage(topapplet.getCodeBase(), "patterns/D.png");
        patternimage[6] = topapplet.getImage(topapplet.getCodeBase(), "patterns/E.png");
        patternimage[7] = topapplet.getImage(topapplet.getCodeBase(), "patterns/F.png");
        patternimage[8] = topapplet.getImage(topapplet.getCodeBase(), "patterns/G.png");
        patternimage[9] = topapplet.getImage(topapplet.getCodeBase(), "patterns/H.png");
        patternimage[10] = topapplet.getImage(topapplet.getCodeBase(), "patterns/I.png");
        patternimage[11] = topapplet.getImage(topapplet.getCodeBase(), "patterns/J.png");
        patternimage[12] = topapplet.getImage(topapplet.getCodeBase(), "patterns/K.png");
        patternimage[13] = topapplet.getImage(topapplet.getCodeBase(), "patterns/L.png");
        patternimage[14] = topapplet.getImage(topapplet.getCodeBase(), "patterns/M.png");
        patternimage[15] = topapplet.getImage(topapplet.getCodeBase(), "patterns/N.png");
        patternimage[16] = topapplet.getImage(topapplet.getCodeBase(), "patterns/O.png");
        patternimage[17] = topapplet.getImage(topapplet.getCodeBase(), "patterns/P.png");
        patternimage[18] = topapplet.getImage(topapplet.getCodeBase(), "patterns/Q.png");
        patternimage[19] = topapplet.getImage(topapplet.getCodeBase(), "patterns/R.png");
        patternimage[20] = topapplet.getImage(topapplet.getCodeBase(), "patterns/S.png");
        patternimage[21] = topapplet.getImage(topapplet.getCodeBase(), "patterns/T.png");
        patternimage[22] = topapplet.getImage(topapplet.getCodeBase(), "patterns/U.png");
        patternimage[23] = topapplet.getImage(topapplet.getCodeBase(), "patterns/V.png");

        //Wait for all patterns to be loaded
        MediaTracker media_tracker = new MediaTracker(topapplet);

        for (i = 1; i <= 23; i++) {
            media_tracker.addImage(patternimage[i], 0);
        }
        System.out.println("Loading patterns");
        try {
            media_tracker.waitForID(0);
        } catch (InterruptedException e) {
            System.out.println("Image loading interrupted : " + e);
        }
        System.out.println("Patterns loaded");

        // try to wait for image to be loaded
        // catch if loading was interrupted


        pieceimage = new Image[number_of_pieces + 1][4][2];
        generate_piece_images();
    }

    public void generate_piece_images() {
        Image tempimage;
        int saverotation;
        e2piece temppiece;
        rownb = topapplet.game.rownb;
        colnb = topapplet.game.colnb;
        e2piecelist all_piece_list = topapplet.game.get_all_pieces();
        all_piece_list.restart();
			// Create a new alpha composite 
			AlphaComposite ac = 
      			AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.5f); 

        while (all_piece_list.hasNext()) {
            temppiece = all_piece_list.next();
            saverotation = temppiece.get_rotation();
            temppiece.reset_rotation();
            i = temppiece.id;
            //generate piece image
            tempimage = topapplet.createImage(piece_w, piece_h);
            pieceimage[i][0][1] = topapplet.createImage(piece_w, piece_h);
            tempgraphic = (Graphics2D) pieceimage[i][0][1].getGraphics();
            tempg2 = (Graphics2D) tempimage.getGraphics();
            tempg2.setColor(Color.gray);
            tempg2.fillRect(0, 0, piece_w, piece_h);
            //tempg2.drawImage(patternimage[temppiece.top], 0, 0, topapplet);
            tempg2.drawImage(patternimage[temppiece.top], 0, 0, piece_w, piece_h, topapplet);
            tempg2.rotate(Math.PI / 2.0, piece_w/2, piece_h/2);
            tempg2.drawImage(patternimage[temppiece.right], 0, 0, piece_w, piece_h, topapplet);
            tempg2.rotate(Math.PI / 2.0, piece_w/2, piece_h/2);
            tempg2.drawImage(patternimage[temppiece.bottom], 0, 0, piece_w, piece_h, topapplet);
            tempg2.rotate(Math.PI / 2.0, piece_w/2, piece_h/2);
            tempg2.drawImage(patternimage[temppiece.left], 0, 0, piece_w, piece_h, topapplet);
            tempg2.rotate(Math.PI / 2.0, piece_w/2, piece_h/2);

            tempgraphic.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            //tempgraphic.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
            tempgraphic.drawImage(tempimage, 0, 0, piece_w, piece_h, topapplet);
            
            //draw smooth borders to give a 3D impression on the piece
            //tempgraphic.setTransform(new AffineTransform());  // set to identity 
            //tempgraphic.setComposite(ac); 
            //tempgraphic.setColor(Color.black);
            //tempgraphic.drawLine(piece_w - 1, 1, piece_w - 1, piece_h - 1);
            //tempgraphic.drawLine(piece_w - 2, 2, piece_w - 2, piece_h - 2);
            //tempgraphic.drawLine(1, piece_h - 1, piece_w - 1, piece_h - 1);
            //tempgraphic.drawLine(2, piece_h - 2, piece_w - 2, piece_h - 2);
            //tempgraphic.setColor(Color.white);
            //tempgraphic.drawLine(1, 1, 1, piece_h - 1);
            //tempgraphic.drawLine(2, 2, 2, piece_h - 2);
            //tempgraphic.drawLine(1, 1, piece_w - 1, 1);
            //tempgraphic.drawLine(2, 2, piece_w - 2, 2);

            //rotation 1
            pieceimage[i][1][1] = topapplet.createImage(piece_w, piece_h);
            tempgraphic = (Graphics2D) pieceimage[i][1][1].getGraphics();
            tempgraphic.rotate(Math.PI / 2.0, piece_w/2, piece_h/2);
            tempgraphic.drawImage(pieceimage[i][0][1], 0, 0, piece_w, piece_h, topapplet);
            tempgraphic.rotate(Math.PI / 2.0 * 3, piece_w/2, piece_h/2);

            //rotation 2
            pieceimage[i][2][1] = topapplet.createImage(piece_w, piece_h);
            tempgraphic = (Graphics2D) pieceimage[i][2][1].getGraphics();
            tempgraphic.rotate(Math.PI / 2.0, piece_w/2, piece_h/2);
            tempgraphic.drawImage(pieceimage[i][1][1], 0, 0, piece_w, piece_h, topapplet);
            tempgraphic.rotate(Math.PI / 2.0 * 3, piece_w/2, piece_h/2);

            //rotation 3
            pieceimage[i][3][1] = topapplet.createImage(piece_w, piece_h);
            tempgraphic = (Graphics2D) pieceimage[i][3][1].getGraphics();
            tempgraphic.rotate(Math.PI / 2.0, piece_w/2, piece_h/2);
            tempgraphic.drawImage(pieceimage[i][2][1], 0, 0, piece_w, piece_h, topapplet);
            tempgraphic.rotate(Math.PI / 2.0 * 3, piece_w/2, piece_h/2);

			//generate the dark pieces (for highlighting)

            //rotation 0 dark
            pieceimage[i][0][0] = topapplet.createImage(piece_w, piece_h);
            tempgraphic = (Graphics2D) pieceimage[i][0][0].getGraphics();
            tempgraphic.drawImage(pieceimage[i][0][1], 0, 0, piece_w, piece_h, topapplet);
  			tempgraphic.setTransform(new AffineTransform());  // set to identity 
  			tempgraphic.setComposite(ac); 
  			tempgraphic.setColor(Color.black); 
  			tempgraphic.fillRect(0,0,piece_w, piece_h); 

            //rotation 1 dark
            pieceimage[i][1][0] = topapplet.createImage(piece_w, piece_h);
            tempgraphic = (Graphics2D) pieceimage[i][1][0].getGraphics();
            tempgraphic.drawImage(pieceimage[i][1][1], 0, 0, piece_w, piece_h, topapplet);
  			tempgraphic.setTransform(new AffineTransform());  // set to identity 
  			tempgraphic.setComposite(ac); 
  			tempgraphic.setColor(Color.black); 
  			tempgraphic.fillRect(0,0,piece_w, piece_h); 

            //rotation 2 dark
            pieceimage[i][2][0] = topapplet.createImage(piece_w, piece_h);
            tempgraphic = (Graphics2D) pieceimage[i][2][0].getGraphics();
            tempgraphic.drawImage(pieceimage[i][2][1], 0, 0, piece_w, piece_h, topapplet);
  			tempgraphic.setTransform(new AffineTransform());  // set to identity 
  			tempgraphic.setComposite(ac); 
  			tempgraphic.setColor(Color.black); 
  			tempgraphic.fillRect(0,0,piece_w, piece_h); 

            //rotation 3 dark
            pieceimage[i][3][0] = topapplet.createImage(piece_w, piece_h);
            tempgraphic = (Graphics2D) pieceimage[i][3][0].getGraphics();
            tempgraphic.drawImage(pieceimage[i][3][1], 0, 0, piece_w, piece_h, topapplet);
  			tempgraphic.setTransform(new AffineTransform());  // set to identity 
  			tempgraphic.setComposite(ac); 
  			tempgraphic.setColor(Color.black); 
  			tempgraphic.fillRect(0,0,piece_w, piece_h); 
  			
  			temppiece.rotate(saverotation);

        }
    }
}
